<?php

namespace app\index\controller;

use app\index\model\Articles;
use app\index\model\Users;
use think\captcha\Captcha;
use think\Controller;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Request;
use think\Validate;

class User extends Controller
{
    public function index() {
        try {
            $users = (new Users)->all();
        } catch (DbException $e) {
            $this->error($e->getMessage());
        }
        if ( !empty($users)) {
            $this->assign('users', $users);
        }
        return $this->fetch();
    }

    public function register(Request $request) {
        if ($request->isPost()) {
            $validate = Validate::make([
                'username' => 'require|min:3|max:32',
                'nickname' => 'require|min:2',
                'password' => 'require|min:6|confirm',
                'captcha'  => 'require|captcha',
            ]);

            $status = $validate->check($request->param());
            if ( !$status) {
                $this->error($validate->getError());
            } else {
                try {
                    $result = Db::name('users')->insert([
                        'username' => $request->param('username'),
                        'nickname' => $request->param('nickname'),
                        'password' => md5($request->param('password')),
                    ]);
                } catch (\Exception $exception) {
                    $this->error($exception->getMessage());
                }

                if ( !empty($result)) {
                    $this->success('注册成功');
                } else {
                    $this->error('注册失败');
                }
            }
        } else {
            return $this->fetch();
        }
    }

    public function login(Request $request) {
        if ($request->post()) {
            $validate = Validate::make([
                'username' => 'require',
                'password' => 'require',
                'captcha'  => 'require|captcha',
            ]);
            $status = $validate->check($request->post());
            if ($status) {
                try {
                    $user = Db::table('users')
                        ->where('username', $request->param('username'))
                        ->where('password', md5($request->param('password')))
                        ->find();
                } catch (DataNotFoundException $e) {
                    $this->error($e->getMessage());
                } catch (ModelNotFoundException $e) {
                    $this->error($e->getMessage());
                } catch (DbException $e) {
                    $this->error($e->getMessage());
                }

                /** @var user $user */
                if ($user) {
                    \session('id', $user['id']);
                    \session('username', $user['username']);
                    $this->success('登录成功', '/');
                } else {
                    $this->error('账号或者密码错误');
                }
            } else {
                $this->error($validate->getError());
            }
        } else {
            return $this->fetch();
        }
    }

    public function logout() {
        \session(null);
        $this->success('退出成功', '/');
    }

    /**
     * 验证码
     * @return \think\Response
     */
    public function code() {
        $config = [
            'length'   => 4,
            'useNoise' => false,
            'codeSet'  => '123456789',
        ];
        $captcha = new Captcha($config);
        return $captcha->entry();
    }

    /**
     * 查看用户
     * @param Request $request
     * @return mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function show(Request $request) {
        $id = $request->param('id');
        $user = (new Users)->find($id);
        if ( !empty($user->article)) {
            $article = $user->article;
        }
        $this->assign('articles', $article);
        $this->assign('user',$user);
        return $this->fetch();
    }

    /**
     * 删除用户
     *
     * @param Request $request
     *
     * @throws DbException
     */
    public function delete(Request $request) {
        $id = $request->param('id');
        $user = Users::get($id,'article');
        $user->together('article')->delete();
        $this->success('会员删除成功并删除相关文章', '/index/user/index');
    }
}
